package com.haipumi.biz.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

import javax.jms.Destination;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.google.gson.Gson;
import com.haipumi.beans.haipumi_skin;
import com.haipumi.beans.haipumi_user;
import com.haipumi.biz.IUserBiz;
import com.haipumi.mapper.CustMapper;
import com.haipumi.utils.Encrypt;
import com.haipumi.utils.ProducerService_UserQueue;

@Repository("userBiz")
public class UserBizImpl implements IUserBiz {

	@Autowired
	CustMapper custMapper;
	
	
	@Autowired
    @Qualifier("queueDestination") 
	private  Destination queueDestination;
	
	@Autowired
	private  ProducerService_UserQueue producerService_UserQueue;
	
	Gson gson=new Gson();

	
	@Override
	/**
	 * bizimpl中的login的方法，
	 */
	public haipumi_user login(haipumi_user user) {
		haipumi_user backuser = null;
		Map<String ,Object> map=new HashMap<String ,Object>();
		String md5=Encrypt.md5(user.getUpwd());
		map.put("ulogon1", user.getUlogon());
		map.put("md51",md5);
		map.put("ulogon2", user.getUlogon());
		map.put("md52",md5);
		map.put("ulogon3", user.getUlogon());
		map.put("md53",md5);
		map.put("ulogon4", user.getUlogon());
		map.put("md54",md5);
		backuser = custMapper.login(map);
		return backuser;
	}

	@Override
	/**
	 * 注册用户的bizimpl方法
	 */
	public haipumi_user adduser(haipumi_user user) {
		haipumi_user backuser = null;
		
		Map<String ,Object> map=new HashMap<String ,Object>();
		map.put("ulogon", user.getUlogon());
		String md5pwd=Encrypt.md5(user.getUpwd());
		map.put("upwd", md5pwd);
		SimpleDateFormat sdf=new SimpleDateFormat("mmss");
		String username="";
		if(user.getUalais()!=null  &&  !"".equals(user.getUalais())){
			username=user.getUalais();
		}else{
			username="用户"+sdf.format(new Date());
		}
		map.put("username",username );
		
		int r=custMapper.addUser(map);
		if(r>0){
			user.setUalais(username);
			backuser=user;
		}
		return backuser;
	}
	

	
	@Override
	public haipumi_user findUser(String ulogon) {
		haipumi_user user = null;
		user = custMapper.findUserInfoByUlogon(ulogon);
		return user;
	}
	
	public boolean activateUser(String ulogon){
		int r=custMapper.activateUser(ulogon);
		if(r>0){
			return true;
		}
		return false;
	}

	@Override
	public haipumi_user findUnActivate(String ulogon) {
		haipumi_user user=custMapper.findUnActivate(ulogon);
		return user;
	}

	@Override
	public haipumi_user newlogin(haipumi_user user) {
		haipumi_user backuser=custMapper.newlogin(user);
		return backuser;
	}

	@Override
	public boolean savebaseinfo(haipumi_user user) {
		int r=custMapper.savebaseinfo(user);
		if(r>0){
			return true;
		}
		return false;
	}

	@Override
	public haipumi_skin changebackground(int uid, int sid) {
		haipumi_skin skin=null;
		Map map=new Hashtable();
		map.put("uid", uid);
		map.put("sid", sid);
		int r=custMapper.changebackground(map);
		if(r>0){
			skin=custMapper.findskin(sid);
		}
		return skin;
	}

	@Override
	public boolean saveTel(int uid, String uqq, String utel) {
		Map<String ,Object> map=new HashMap<String ,Object>();
		map.put("uid", uid);
		map.put("uqq", uqq);
		map.put("utel", utel);
		int r=custMapper.saveTel(map);
		if(r>0){
			return true;
		}
		return false;
	}

	
	public boolean sendMail(haipumi_user user,String localhref){
	// 发送邮件
//	String smtp = "smtp.163.com";
//	SendMail sm = new SendMail(smtp);
//	String from = "18373368832@163.com";
//	String to = user.getUlogon();
//	String subject = "haipumi嗨扑米欢迎您";
//	
////	localhref="127.0.0.1/haipumiBate2.0/";
//	localhref=localhref.substring(0, 25);
//	
//	String content = "<center style=\"border:blue 1px solid;background:#999; color:#fff;\">尊敬的"+user.getUlogon()+"<br/>您的嗨扑米账号已经注册成功。<br/>嗨扑米，帮助他人，手留余香，请谨记您的账号信息：<br/>登录名："+user.getUlogon()+"<br/>昵称:"+user.getUlogon()+
//			"<br/>密码：<font color=\"#999\">"+user.getUpwd()+"</font><br/><font color=\"#fff\">（请单击以选中密码查看，请谨记您的密码）</font><br/>以上为用户基本信息，请点击下面的链接激活嗨扑米账号，以正常使用。<br/>"+
//			"<font>"+localhref+"cust.action?op=activateuser&ulogon="+user.getUlogon()+"</font>"+
//			"<br/>用户信息不满意？请到用户个人管理页面进行更改<br/><font color=\"red\">邮件为系统发送，仅供激活用户账号，请勿回复，谢谢合作。</font> </center>";
//	//"<a href=\""+localhref+"cust.action?op=activateuser&ulogon="+user.getUlogon()+
//	System.out.println(content);
//	String username = "18373368832@163.com";
//	String password = "srjmkumterdkyiwz";
//	boolean mailflag=sm.send(smtp, from, to, subject, content, username, password);
	try {
		String str=gson.toJson(user)+"%00"+localhref;
		producerService_UserQueue.sendMessage(queueDestination,str);
	} catch (Exception e) {
		e.printStackTrace();
		return false;
	}   
	return true;
}

	@Override
	public haipumi_user changePwd(haipumi_user user) {
		String oldPwd=user.getUpwd();
		String md5pwd=Encrypt.md5(user.getUpwd());
		user.setUpwd(md5pwd);
		int result=custMapper.changePwd(user);
		if(result>0){
			user.setUpwd(oldPwd);
			return user;
		}
		return null;
	}
}
